import {Request} from "express";
import {Response} from "express";
import {exp} from "./exp";

export type onCheckDataFunc=(url:string,
                             modClass:{ new():any;}|null,
                             req:Request, resp:Response, next:(data:any)=>void, router?:RouterPara)=>void|Promise<void>;


export type onSendFunc=(url:string,
                        modClass:{ new():any;}|null,
                        req:Request, resp:Response, dat:any)=>void|Promise<void>;

export type onCatchFunc=(url:string,
                         modClass:{ new():any;}|null,
                         req:Request, resp:Response, err:Error)=>void|Promise<void>;


export interface RouterPara {
    /**
     * 路由目录
     */
    menu:string,
    /**
     * checkObj入参检测回调
     */
    onCheckData:onCheckDataFunc,

    /**
     * 异常捕获回调
     */
    onCatch:onCatchFunc,

    /**
     * 数据发送前的回调
     */
    onSend?:onSendFunc,
    /**
     * 路由额外参数
     */
    extPara?:any,
}

export class ExpFunc {
    static onCheckData:onCheckDataFunc = (url, modReq, req, resp, next)=> {
        next(req.query);
    }


    static onCatch:onCatchFunc = (url, modReq, req, resp, err)=> {
        console.error(url + exp.netErr + " : " + JSON.stringify(req.query));
        console.error(err);
        resp.send(exp.netErr);
    }

    static onSend:onSendFunc = (url, modReq, req, resp, dat)=> {
        resp.send(dat);
    }

}